package coml.jiyun.hp.gjtk_b.fragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.gjtk_b.R;
import coml.jiyun.hp.gjtk_b.adapter.HomeAdapter;
import coml.jiyun.hp.gjtk_b.bean.WanBean;
import coml.jiyun.hp.gjtk_b.model.ImpWanModel;
import coml.jiyun.hp.gjtk_b.presenter.ImpWanPresenter;
import coml.jiyun.hp.gjtk_b.view.WanView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements WanView {

    private RecyclerView rlv;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private HomeAdapter homeAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        return inflate;
    }

    private void initData() {
        ImpWanPresenter impWanPresenter = new ImpWanPresenter(new ImpWanModel(), this);
        impWanPresenter.getData();
    }

    private void initView(View inflate) {
        rlv = inflate.findViewById(R.id.rlv);

        datasBeans = new ArrayList<>();
        homeAdapter = new HomeAdapter(getActivity(), datasBeans);
        rlv.setAdapter(homeAdapter);
        rlv.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    @Override
    public void onSuccess(WanBean wanBean) {
        List<WanBean.DataBean.DatasBean> datas = wanBean.getData().getDatas();
        datasBeans.addAll(datas);
        homeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {
        Log.d("TAG", "onFail: "+str);
    }
}
